package com.example.demo08;

import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Locale;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

import com.edroid.common.utils.FileUtils;
import com.edroid.common.utils.IpUtils;
import com.edroid.common.utils.PhoneUtils;
import com.edroid.common.utils.TimeUtils;

public class InfoPage extends BaseTextPage {
	
//	 private static final int a = Build.VERSION.SDK_INT;
//	  private static final String b = Build.PRODUCT.toLowerCase();
//	  private static final String c = Build.MODEL.toLowerCase();
//	  private static final String d = Build.BRAND.toLowerCase();
//	  private static final String e = Build.MANUFACTURER.toLowerCase();
//	  private static final String f = Build.HOST.toLowerCase();
//	  private static final String g = Build.DISPLAY.toLowerCase();
//	  private static final String h = Build.FINGERPRINT.toLowerCase();
	  
	  void ccc() {
		  System.out.println(Build.PRODUCT);
		  System.out.println(Build.MODEL);
		  System.out.println(Build.BRAND);
		  System.out.println(Build.MANUFACTURER);
		  System.out.println(Build.HOST);
		  System.out.println(Build.DISPLAY);
		  System.out.println(Build.FINGERPRINT);
//		  System.out.println(h);
		  
//		  SystemSensorManager 
	  }

	private void showInfo(Context c) {
		ccc();
		
		TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
		StringBuilder sb = new StringBuilder(2048);

		WifiManager wifi = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);

		String android_id = Secure.getString(c.getContentResolver(), Secure.ANDROID_ID);
		WifiInfo wifiInfo = wifi.getConnectionInfo();

		sb
		.append("当前时间: ").append(TimeUtils.getDateTimeNow()).append('\n')
		.append("厂商: ").append(Build.MANUFACTURER).append("  机型：").append(Build.MODEL).append('\n')
		.append("版本: ").append(Build.VERSION.SDK_INT).append('/')
			.append(Build.VERSION.RELEASE).append('\n')
		.append("增量: ").append(Build.VERSION.INCREMENTAL).append('/')
			.append(Build.VERSION.CODENAME).append('\n')
		.append("imei: ").append(tm.getDeviceId()).append('\n')
		.append("imsi: ").append(tm.getSubscriberId()).append('\n')
		.append("sim: ").append(tm.getSimSerialNumber()).append('\n')
		.append('\n');
		
		sb.append("GFS_ID: ").append(PhoneUtils.getGFS_ID(c)).append('\n')
		.append("MAC: ").append(wifiInfo.getMacAddress()).append('\n')
		.append("BSSID: ").append(wifiInfo.getBSSID()).append('\n')
		.append("SSID: ").append(wifiInfo.getSSID()).append('\n')
		.append("android_id: ").append(android_id).append('\n').append('\n');
		
		sb
		.append("local_ip: ").append(IpUtils.getLocalIpV4()).append('\n')
		.append("net.hostname: ").append(PhoneUtils.getHostname()).append('\n')

		.append("ro.serialno: ").append(PhoneUtils.getSerial()).append('\n')
		.append("ro.boot.serialno: ").append(PhoneUtils.getSystemProperties("ro.boot.serialno")).append('\n')
		.append("ro.boot.mode: ").append(PhoneUtils.getSystemProperties("ro.boot.mode")).append('\n')
		.append("dhcp.wlan0.dns1: ").append(PhoneUtils.getSystemProperties("dhcp.wlan0.dns1")).append('\n')
		.append("dhcp.wlan0.ipaddress: ").append(PhoneUtils.getSystemProperties("dhcp.wlan0.ipaddress")).append('\n')
		.append("gsm.current.phone-type: ").append(PhoneUtils.getSystemProperties("gsm.current.phone-type")).append('\n')
		.append("dhcp.wlan0.pid: ").append(PhoneUtils.getSystemProperties("dhcp.wlan0.pid")).append('\n')
		.append("ro.boot.baseband: ").append(PhoneUtils.getBaseband()).append('\n')
		// 基带
		.append("ro.boot.bootloader: ").append(PhoneUtils.getBootloader()).append('\n')
		.append("ro.boot.hardware: ").append(PhoneUtils.getHardware())
		.append('\n');
		
		
		//
		sb.append("Locale..getCountry = ").append(Locale.getDefault().getCountry()).append('\n');
		sb.append("Locale..getLanguage = ").append(Locale.getDefault().getLanguage()).append('\n');
		
		//
		sb.append("java env:").append('\n')
		.append(java.lang.System.getenv()).append('\n');

		sb.append("java Properties:").append('\n')
		.append(java.lang.System.getProperties()).append('\n');
		
		{
			BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
			if(bt != null)
				sb.append("bluetooth adderse: " + bt.getAddress()).append('\n');
		}

		{
			DisplayMetrics dm = getResources().getDisplayMetrics();
			sb.append("resolution: ").append(dm.widthPixels).append(' ').append(dm.heightPixels).append(' ').append(dm.densityDpi).append(' ').append(dm.density).append('\n');
			sb.append("mcc=").append(getResources().getConfiguration().mcc).append('\n');
			sb.append("mnc=").append(getResources().getConfiguration().mnc).append('\n');
		}

		{
			ConnectivityManager connectivity = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);

			NetworkInfo info = connectivity.getActiveNetworkInfo();
			if (info != null) { // 有联网 且 可以联网
				sb.append('\n').append("active: ").append(info.toString()).append("type=").append(info.getType()).append("\n\n");
			}

			NetworkInfo info2 = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			if (info2 != null) {
				sb.append("mobile: ").append(info2.toString()).append("\n\n");
			}

			NetworkInfo info3 = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (info3 != null)
				sb.append("wifi: ").append(info3.toString()).append("\n\n");
		}

		sb.append("\nDeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion());
		sb.append("\nLine1Number = " + tm.getLine1Number());
		sb.append("\nNetworkCountryIso = " + tm.getNetworkCountryIso());
		sb.append("\nNetworkOperator = " + tm.getNetworkOperator());
		sb.append("\nNetworkOperatorName = " + tm.getNetworkOperatorName());
		sb.append("\nNetworkType = " + tm.getNetworkType());
		sb.append("\nPhoneType = " + tm.getPhoneType());
		sb.append("\nSimCountryIso = " + tm.getSimCountryIso());
		sb.append("\nSimOperator = " + tm.getSimOperator());
		sb.append("\nSimOperatorName = " + tm.getSimOperatorName());
		sb.append("\nSimState = " + tm.getSimState());
		sb.append('\n').append('\n');

		sb.append(FileUtils.fileToString("/proc/version"));
		sb.append('\n').append('\n');
		sb.append(FileUtils.fileToString("/proc/cpuinfo"));
		sb.append('\n').append('\n');
		sb.append(FileUtils.fileToString("/sys/class/net/wlan0/address"));
		sb.append('\n').append('\n');

		{
			Field[] fields = Build.class.getDeclaredFields();
			for (Field field : fields) {
				field.setAccessible(true);
				try {
					sb.append(field.getName()).append('=').append(field.get(null)).append('\n');
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		try {
			int j=0, k=0;
			
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface i = en.nextElement();
				
				System.out.println("j" + (++j) + " " + i);
				k = 0;
				for (Enumeration<InetAddress> e = i.getInetAddresses(); e.hasMoreElements();) {
					InetAddress a = e.nextElement();
					
					System.out.println("k" + (++k) + "=" + a);
					
					if (!a.isLoopbackAddress()) {
						String ip = a.getHostAddress();
						System.out.println("lookback " + ip);;
					}
				}
			}
		} catch (SocketException ex) {
			ex.printStackTrace();
		}
		
//		{
//			final Uri a = Uri.parse("content://com.facebook.katana.provider.AttributionIdProvider");
//		    String[] arrayOfString = {"aid"};
//		    Cursor localCursor = getActivity().getContentResolver().query(a, arrayOfString, null, null, null);
//		    if ((localCursor != null) && (!localCursor.moveToFirst())) {
//		      	sb.append("aaaid=").append(localCursor.getString(localCursor.getColumnIndex("aid")));
//		    } else {
//		    	sb.append("aaaid=null");
//		    }
//		}

		String msg = sb.toString();

		Log.i("", msg);
		
		set(msg);
		
		update();
	}
	
	
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		
		showInfo(getActivity());
	}
}
